﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dzzzr;
using Nightwish.Controls.Tree;

namespace Dzzzr
{
	/// <summary>
	/// Manage entity tree
	/// </summary>
	public class EntityBrowserModel : ITreeModel
	{
		/// <summary>
		/// Associated team
		/// </summary>
		Team team = null;

		/// <summary>
		/// Init object
		/// </summary>
		/// <param name="team"></param>
		public void Init(Team team)
		{
			this.team = team;
		}

		/// <summary>
		/// Associated team
		/// </summary>
		public Team Team
		{
			get
			{
				return team;
			}
		}

		/// <summary>
		/// Return child elements
		/// </summary>
		/// <param name="treePath"></param>
		/// <returns></returns>
		public System.Collections.IEnumerable GetChildren(TreePath treePath)
		{
			if (treePath.IsEmpty())
			{
				// root items
				if (team != null)
				{
					foreach (Entity entity in (from e in team.Entities orderby e.EntityTypeID, e.Name select e))
					{
						yield return entity;
					}
				}
			}
		}

		/// <summary>
		/// est leaf element
		/// </summary>
		/// <param name="treePath"></param>
		/// <returns></returns>
		public bool IsLeaf(TreePath treePath)
		{
			return true;
		}

		#region Events

		/// <summary>
		/// Event
		/// </summary>
		public event EventHandler<TreeModelEventArgs> NodesChanged;

		/// <summary>
		/// Event
		/// </summary>
		public event EventHandler<TreeModelEventArgs> NodesInserted;

		/// <summary>
		/// Event
		/// </summary>
		public event EventHandler<TreeModelEventArgs> NodesRemoved;

		/// <summary>
		/// Event
		/// </summary>
		public event EventHandler<TreePathEventArgs> StructureChanged;

		/// <summary>
		/// Fire event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void FireNodesChanged(object sender, TreeModelEventArgs e)
		{
			if (NodesChanged != null)
				NodesChanged(sender, e);
		}

		/// <summary>
		/// Fire event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void FireNodesInserted(object sender, TreeModelEventArgs e)
		{
			if (NodesInserted != null)
				NodesInserted(sender, e);
		}

		/// <summary>
		/// Fire event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void FireNodesRemoved(object sender, TreeModelEventArgs e)
		{
			if (NodesRemoved != null)
				NodesRemoved(sender, e);
		}

		/// <summary>
		/// Fire event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void FireStructureChanged(object sender, TreePathEventArgs e)
		{
			if (StructureChanged != null)
				StructureChanged(sender, e);
		}
		#endregion
	}
}
